package cn.lysj.framework.customer.util;

import cn.lysj.framework.common.utils.random.RandomUtil;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.customer.domain.Customer;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


/**
 * @ Author ：drj.
 * @ Date  ：Created in 20:31 2018/11/5
 * @ Description:
 **/
@RestController
public class AliPayAuzLoginUtil {

    //支付宝网关（固定）
    public static final String URL = "https://openapi.alipay.com/gateway.do";

    //应用id
    public static final String APP_ID = "2018110561995726";

    //参数返回格式
    public static final String FORMAT = "json";

    //编码
    public static final String CHARSET = "utf-8";

    //应用私钥
    public static final String APP_PRIVATE_KEY =
            "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCwU40zuF67VwhM" +
                    "VOx0P+7FDxcl02Iwu8XBIU5SJFvW+d4cHIt5WjTuVRJ/u5ob0Fxb/VH02jFi/59C" +
                    "knt495v38H9ouuL5T/KV3bZBSLLUyhwYUZ0Xg1inIqmOf8jIFqsL10MehC2GQAAI" +
                    "LRVDlbGA4nfnimld+tRXDo2poynwNoT8r/2gBC8hSik4qK6l8up0T2V0GTZ+Umr5" +
                    "1K9Z2hwQWUuWRp6cIGRLQzPXTiU0rj/0yLQGsuGPql5RMoKsy1iii6CryiGkkUTv" +
                    "umm1KxkJy1pq4tXXAH6fTtAFzKRyTocm+qFOI4qzRKfDUGP2PM6fsz5LrQdQoFlV" +
                    "X5afFX2HAgMBAAECggEAa/2f+08t5br2ABn6/2+wgswtNyNxfbti8Fe/qhtOjClY" +
                    "WQs6eMZAqL89pzF0vxgrmMNbEIUZvjIkjhc8u+eahI2s2q2rGVpXjmXNm8o0jJKE" +
                    "Bnjlp2LwRty5z+Z/R9xqQcRC/UuivhgwbNF/tnbV0cPh/9MLmV1n8zGcnYqqwsz8" +
                    "0NE/V11kFpGIuaV7b0pdxnndx1euQRm9688PFqqzEQ96BoBWqPuX0yoojRDJ9fl5" +
                    "l0ibq9aTVNhoIv5A1J08+DMAp/pBhL1GYWjOh2c3jx00Q9kVxyN8hk9c9xTw3bNA" +
                    "peJhvSwOm5z3bZK/HpNqjkUKI/Ag/TMeMKjtU7t8IQKBgQDc3CYTOVSmskNIGan8" +
                    "Qv0l1AQid4r2dy+wRYvriVMo8N537zb0KOAIWvg2j6RckzI26Ghd3xrJua0ryrxb" +
                    "h/9Ca+4onOnHY3QoWPN32vAOmoYvKj6A62Y9ril3Npk4lPgYNglNISMXB2Ww1a1D" +
                    "5Cfxf51lVTDaThAY+iRXMgctGQKBgQDMYYEtQFlocSJPLBgPxKwVKX/t9cxmXg4V" +
                    "ZvbDf1YsKBLo1yYibLS6FfYjaZxyeNJvOBB3FnlX/U4AFEEw70+XSDSvkOEvVjuK" +
                    "UwxJZT8+fV+ac9k+90DjKUXhGrRFG5lPkSN83siH7hPHzd+pKeF5tQbh0m9WXoVP" +
                    "hlBls9iznwKBgQCqXEYiXf6l9qtfWix8oU+Tdk8aJwvRwW3PklWzgWHcGq//402I" +
                    "WF83ErSToYkuv3vkTrDyd726DLc/KErSb7y+B3LVcPQDEPvwg2yJ7x9spFUM3JEJ" +
                    "8IoU2dqyUjMuKfqU8lmKXR0sDlCqbqgPzlXhOoa2O1hLjBtuqMSPXc+bQQKBgQDI" +
                    "T6gzmaYHXxqpTiYnDjM6anof15CFpBlZkJi58suaa4xc+ME/bBuVZoDfJFtZNmHI" +
                    "78Hzdetn+bPBEBj0xRfUxcgdannJ01J3t/2K0diyfl59Gp4JwHuwKHt8rJeSRAJ2" +
                    "oWGOuSsBTq3EXlqfiZSbnHkqSv0kQW+XCaUyiJoTMQKBgE2kkLBjdcr1mVOUPiKc" +
                    "mu/YTzJOigc4vRDnPUl8j1TcY9S5vYiaH1XiLZKE+h9UUcpmW+J1j0ZhTiD/T4xp" +
                    "vuRad+N+Syv2fKpmDoB87O5VGs0gRWOs+U9pMINtaLf6mi23WEI3a5CO8LJpmPSi" +
                    "iz10BwHCvlNkEykYOWcAPI9x";

    //支付宝公钥
    public static final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkFqa88BsObTX14U7sye6S9vkb1hZxFa5ZCTq29U6uDod2Z1RrE4TzlOKxaFrztGJQ9Ig2o3qlIFhoeoGymiYt0IcYtIaQfwacHsYbQHwQz9o7mGm4FfuBVI/ZNTzVxBky8/TU89tRvHwKPWpjXK/uNLyDOr44Q3dsF4RrCRK0+Xa6nD529JcB0oPETacTpEF4x9e+ELTkZHNnIXH282Y++Y/YVEoOVOZYCHaTIDGgGot8MOjxTQbUm1CCd9AchQrvEXMC4Qn1By69tTLfls7jGw3PJZhcElV1qmSLqdvqTEOlcztOJ+Ouni1fLyoVL3OhNZKxO5OoNsaxIZ8gN3EfQIDAQAB";


    /**
     * @author drj
     * @date 2018/11/8 17:39
     * @Description:阿里授权登录 获取用户信息
     */
    public static Customer getUserInfo(String code) {
        Customer aliPayCustomer = new Customer();
        if (StringUtils.isNotBlank(code)) {
            try {
                AlipayClient alipayClient = new DefaultAlipayClient(URL, APP_ID, APP_PRIVATE_KEY, FORMAT, AlipayConfig.CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
                //使用auth_code获取access_token与user_id
                AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
                request.setCode(code);
                request.setGrantType("authorization_code");
                AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(request);
                //拉取用户信息
                if (oauthTokenResponse.isSuccess()) {
                    AlipayUserInfoShareRequest requestUser = new AlipayUserInfoShareRequest();
                    AlipayUserInfoShareResponse userinfoShareResponse = alipayClient.execute(requestUser, oauthTokenResponse.getAccessToken());
                    if (userinfoShareResponse != null) {
                        aliPayCustomer.setHeadPhoto(userinfoShareResponse.getAvatar());//用户头像
                        if (ParamUtil.isBlank(userinfoShareResponse.getNickName())) {
                            aliPayCustomer.setNickName("点餐用户：" + RandomUtil.getRandomNum(6));
                        } else {
                            aliPayCustomer.setNickName(userinfoShareResponse.getNickName());//用户昵称
                        }
                        aliPayCustomer.setFlagId(userinfoShareResponse.getUserId());//用户id
                        aliPayCustomer.setCity(userinfoShareResponse.getCity());//城市
                        aliPayCustomer.setCreateTime(new Date());
                        aliPayCustomer.setCustomerType(1); //设置用户类型为阿里用户u
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return aliPayCustomer;
    }


}
